﻿
Public Class frmMailSetup
    Friend SmtpSetup As Config.SmtpSetup

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Friend Sub LoadSmtpSetup()
        SmtpSetup = Config.SmtpConfig.Load(False)
    End Sub

    Private Sub frmMailSetup_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadSmtpSetup()
        InitAppConfigValue()
    End Sub

    Private Sub InitAppConfigValue()
        cbCheckInteval.Text = CStr(SmtpSetup.CheckInteval)
        cbSendInteval.Text = CStr(SmtpSetup.SendInteval)
        Dim SelectedText As String = String.Empty
        cbFrom.Items.Clear()
        If SmtpSetup.Accounts Is Nothing Then
            Return
        End If

        For i As Integer = 0 To SmtpSetup.Accounts.Count - 1
            If SmtpSetup.Accounts(i).Name Is Nothing Then
                cbFrom.Items.Add(String.Concat("(Mail)", SmtpSetup.Accounts(i).Mail))
            Else
                cbFrom.Items.Add(SmtpSetup.Accounts(i).Name)
            End If
        Next

        cbFrom.SelectedIndex = cbFrom.FindStringExact(SmtpSetup.SelectedAccount)
    End Sub

    Private Sub SaveAppConfigValue()
        SmtpSetup.CheckInteval = CInt(cbCheckInteval.Text)
        SmtpSetup.SendInteval = CInt(cbSendInteval.Text)
        SmtpSetup.SelectedAccount = cbFrom.Text
        Config.SmtpConfig.Save()
    End Sub

    Private Sub btnConfirm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConfirm.Click
        Try
            SaveAppConfigValue()
            MsgBox("保存成功", MsgBoxStyle.Information, "提示")
            Close()
        Catch ex As Exception
            MsgBox(String.Format("修改失败，原因：{0}", ex.Message), MsgBoxStyle.Exclamation)
        End Try
    End Sub

    Private Sub cbFrom_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbFrom.SelectedIndexChanged
        Dim Account As Config.Account = SmtpSetup.Accounts.Find(cbFrom.Text)
        If Not Account Is Nothing Then
            tbSmtpServer.Text = Account.Server
            tbSmtpPort.Text = CStr(Account.Port)
            tbUserName.Text = Account.User
            mtbPassword.Text = Account.Passowrd
            tbMailAddr.Text = Account.Mail
            tbDisplayName.Text = Account.DisplayName
        Else
            ClearAcount()
        End If
    End Sub

    Private Sub ClearAcount()
        tbSmtpServer.Text = String.Empty
        tbSmtpPort.Text = CStr(25)
        tbUserName.Text = String.Empty
        mtbPassword.Text = String.Empty
        tbMailAddr.Text = String.Empty
        tbDisplayName.Text = String.Empty
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If cbFrom.FindStringExact(cbFrom.Text) = -1 Then
            cbFrom.SelectedIndex = cbFrom.Items.Add(cbFrom.Text)
        Else
            MsgBox(cbFrom.Text + " 已经存在请更换其他名称!!!", MsgBoxStyle.OkOnly, "提示")
        End If

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

        If MsgBox("是否确定删除此配置?", MsgBoxStyle.YesNo, "提示") = MsgBoxResult.Yes Then
            Dim Account As Config.Account = SmtpSetup.Accounts.Find(cbFrom.Text)
            If Not Account Is Nothing Then
                SmtpSetup.Accounts.Remove(Account)
            End If
            cbFrom.Items.Remove(cbFrom.Text)
            ClearAcount()
            cbFrom.SelectedIndex = cbFrom.Items.Count - 1
            Account = Nothing
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Button2.Enabled = False
        If Not String.IsNullOrEmpty(cbFrom.Text) Then
            Dim Account = SmtpSetup.Accounts.Find(cbFrom.Text)
            If Account Is Nothing Then
                Account = New Config.Account
                SmtpSetup.Accounts.Add(Account)
                cbFrom.Items.Add(cbFrom.Text)
            End If
            Account.Name = cbFrom.Text
            Account.Server = tbSmtpServer.Text
            Account.Port = CInt(tbSmtpPort.Text)
            Account.User = tbUserName.Text
            Account.Passowrd = mtbPassword.Text
            Account.Mail = tbMailAddr.Text
            Account.DisplayName = tbDisplayName.Text
            Account = Nothing
        End If
        Button2.Enabled = True
    End Sub

    Protected Overrides Sub Finalize()
        MyBase.Finalize()
    End Sub

End Class